import { _decorator, AudioSource, Component, director, Node, tween, Vec3 } from 'cc';
import { GameState } from './GameState';
import { baseScene } from './baseScene';
import { Editor } from './Editor';
import { LayerRoot } from './LayerRoot';
const { ccclass, property } = _decorator;

@ccclass('mainScene')
export class mainScene extends baseScene {
    @property(Node)
    editorRoot: Node = null;
    @property(Node)
    sheeps: Node = null;
    audioSource: AudioSource = null;
    start() {
        this.playSound();
        if(GameState.gameMode === 0){
            this.startGame();
            this.editorRoot.active = false;
        }else{
            this.editorRoot.active = true;
            this.editorRoot.getChildByName('Editor').getComponent(Editor).initGrid();
        }
    }
    onSoundClick() {
        GameState.soundOn = !GameState.soundOn;
        this.playSound();
    }
    
    sheepsRun(callback) {
        this.sheeps.setPosition(350, 0)
        this.sheeps.setScale(2,2)
        this.sheeps.active = true;
        tween(this.sheeps)
        .to(0.8, {
            position: new Vec3(-350, 0)
        }).call(() => {
            callback();
        })
        .to(0.2, {
            position: new Vec3(-700, 0)
        }).call(() => {
            this.sheeps.active = false;
        }).start();

    }

    onBackClick() {
        director.loadScene('home');
        this.stopSound();
    }

    startGame() {
        this.sheepsRun(() => {
            GameState.resetTimes();
            const layerRoot = this.node.getChildByName('LayerRoot');
            layerRoot.getComponent(LayerRoot).startGame(GameState.curLevel);
        })
    }

    nextLevel() {
        GameState.curLevel += 1;
        if(GameState.curLevel > GameState.getMaxLevel()){
            GameState.curLevel = 1;
        }
        this.startGame();
    }
}


